package com.crm.service.before_sale.impl;

import com.crm.dao.before_sale.SchemeDAO;
import com.crm.exception.ServiceException;
import com.crm.pojo.Scheme;
import com.crm.service.before_sale.SchemeService;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SchemeServiceimpl implements SchemeService {
    @Autowired
    private SchemeDAO schemeDAO;
    /**
     * 分页展示解决方案
     * @param scheme
     * @param curpage
     * @param pagesize
     * @param submitStartTime
     * @param submitEndTime
     * @return
     */
    @Override
    public List<Scheme> showSchemeList(Scheme scheme, int curpage, int pagesize, Date submitStartTime, Date submitEndTime) {
        Assert.notNull(scheme);
        PageRequest pageRequest = new PageRequest(curpage-1,pagesize, Sort.Direction.DESC,"scId");
        @SuppressWarnings("unchecked")
        Page<Scheme> page = schemeDAO.findAll(new Specification<Scheme>() {
            @Override
            public Predicate toPredicate(Root<Scheme> root, CriteriaQuery<?> argl, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                //根据以下条件查询
                //1.方案主题
                if(!StringUtils.isEmpty(scheme.getScTheme())){
                    list.add(cb.like(root.get("scTheme").as(String.class),"%" + scheme.getScTheme() + "%"));
                }
                //2.审核
                if(!StringUtils.isEmpty(scheme.getScCheck())){
                    list.add(cb.like(root.get("importance").as(String.class),"%" + scheme.getScCheck()+ "%"));

                }

                //没有被删除
                list.add(cb.notLike(root.get("schemeIsDel").as(String.class), "是"));

                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageRequest);
        List<Scheme> list = page.getContent();
        return list;
    }

    /**
     * 获取解决方案总数量
     * @param scheme
     * @param submitStartTime
     * @param submitEndTime
     * @return
     */
    @Override
    public Long getSchemeCount(Scheme scheme, Date submitStartTime, Date submitEndTime) {
        Assert.notNull(scheme);
        Long total = schemeDAO.count(new Specification<Scheme>() {
            @Override
            public Predicate toPredicate(Root<Scheme> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                //根据以下条件查询
                //1.方案主题
                if(!StringUtils.isEmpty(scheme.getScTheme())){
                    list.add(cb.like(root.get("scTheme").as(String.class),"%" + scheme.getScTheme() + "%"));
                }
                //2.审核
                if(!StringUtils.isEmpty(scheme.getScCheck())){
                    list.add(cb.like(root.get("importance").as(String.class),"%" + scheme.getScCheck()+ "%"));

                }

                //没有被删除
                list.add(cb.notLike(root.get("schemeIsDel").as(String.class), "是"));

                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        });
        return total;
    }

    /**
     * 根据id删除解决方案
     * @param scId
     * @return
     */
    @Override
    public Result deleteScheme(Long scId) throws ServiceException {
        try {
            schemeDAO.deleteSchemeById(scId);
            return Result.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }            return new Result("Sorry,删除失败!");

    }

    /**
     * 添加一个解决方案
     * @param scTheme
     * @param clientId
     * @param chanceId
     * @param chragePeopleId
     * @param submitTime
     * @param scCheck
     * @param clientInfoFeedback
     * @param schemeContent
     * @return
     */
    @Override
    public Result addScheme(String scTheme, Long clientId, Long chanceId, Long chragePeopleId, Date submitTime, String scCheck, String clientInfoFeedback, String schemeContent) {
        try {
            Scheme scheme = new Scheme();
            scheme.setScTheme(scTheme);
            scheme.setClientId(clientId);
            scheme.setChanceId(chanceId);
            scheme.setChragePeopleId(chragePeopleId);
            scheme.setSubmintTime(new Date());
            scheme.setScCheck(scCheck);
            scheme.setClientInfoFeedback(clientInfoFeedback);
            scheme.setSchemeContent(schemeContent);
            scheme.setSchemeIsDel("否");
            schemeDAO.save(scheme);
            return  Result.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("Sorry,添加失败!");
        }

    }

    /**
     * 编辑解决方案
     * @param scheme
     * @return
     */
    @Override
    public Result editScheme(Scheme scheme) {
        try {
            scheme.setSubmintTime(new Date());
            schemeDAO.save(scheme);
            return Result.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("Sorry,修改失败!");
        }
    }
}
